"""
auth:xgt-python
datetime:2021/11/8
Python爬虫之回车桌面壁纸
"""
import re
import requests
import parsel  # 数据提取模快
import os

# url = 'https://mm.enterdesk.com/      '

response = requests.get('https://mm.enterdesk.com/')
# 1. 获取网页源代码
# print(response.text)
data_html = response.text

# 2. 提取需求的数据
url_list = re.findall('<div><a href="(.*?)" target="_blank">(.*?)</a>', data_html)
for url, title in url_list:
    print(f'================正在爬取{title}==================')
    # 创建文件夹
    if not os.path.exists('img/' + title):
        os.mkdir('img/' + title)


    # print(url,title)
    # 3.通过 requests 向每一个壁纸详情页发送请求
    response_2 = requests.get(url)
    # 4. 获取数据 网页源代码
    data_html_2 = response_2.text
    # 5. 提取 所有小图片链接
    # css选择器 html 基础
    selector = parsel.Selector(data_html_2)
    # .swipwe-wrapper div img::attr()
    # 但是我们需要的是 所有小图链接
    img_url_list = selector.css('.swiper-wrapper div img::attr(src)').getall()
    # 6. 每个小图片转换成大图链接
    # python语言中我们现在拿到的数据 列表
    for img_url in img_url_list:
        img_url = img_url.replace('360_360', 'source')
        # 7. 向每一个大图片发送网络请求 获取图片二进制数据
        img_data = requests.get(img_url).content
    # 保存图片
    img_title = img_url.split('/')[-1]
    with open(f'img/{title}/{img_title}',mode='wb') as file:
        file.write(img_data)
    print(img_title, '爬取成功！')

